--- title: PivotPy keywords: fastai sidebar: home_sidebar summary: "A Python Processing Tool for Vasp Input/Output. A CLI is available in Powershell, see Vasp2Visual." description: "A Python Processing Tool for Vasp Input/Output. A CLI is available in Powershell, see Vasp2Visual." nb_path: "index.ipynb" ---
{% raw %}
{% endraw %}

Install

pip install pivotpy

How to use

New: Ipywidgets-based GUI in Progress

See GIF here: GIF

  • The code at end is used below to rebuild dataframe which can be use in many ways such as generating latex table. The matplotlib code is used to generate publication quality figure.
{% raw %}
import pivotpy as pp
paths = ['e:/Research/graphene_example/ISPIN_1/bands/DOS/vasprun.xml',
         'e:/Research/graphene_example/ISPIN_1/bands/vasprun.xml',
         'e:/Research/graphene_example/ISPIN_1/dos/vasprun.xml',
         'e:/Research/graphene_example/ISPIN_2/bands/vasprun.xml',
         'e:/Research/graphene_example/ISPIN_2/dos/sigm0_01/vasprun.xml',
         'e:/Research/graphene_example/ISPIN_2/dos/vasprun.xml',
         'e:/Research/graphene_example/vasprun.xml']
df = pp.generate_summary(paths_list=paths)
df
Root Path: e:/Research/graphene_example/
sys V a b c VBM CBM so_max so_min E_gap Δ_SO rel_path
0 C2 105.493250 2.468030 2.468030 19.998290 -3.946800 -0.812500 -3.949100 4.781000 3.134300 -8.730100 ISPIN_1/bands/DOS
1 C2 105.493250 2.468030 2.468030 19.998290 -2.773300 -1.168200 nan nan 1.605100 nan ISPIN_1/bands
2 C2 105.493250 2.468030 2.468030 19.998290 4.516100 4.559100 -9.840500 -12.235500 0.043000 2.395000 ISPIN_2/bands
{% endraw %} {% raw %}
print(df.caption)
df.data
Root Path: e:/Research/graphene_example/
sys V a b c VBM CBM so_max so_min E_gap Δ_SO rel_path
0 C2 105.49325 2.46803 2.46803 19.99829 -3.9468 -0.8125 -3.9491 4.7810 3.1343 -8.7301 ISPIN_1/bands/DOS
1 C2 105.49325 2.46803 2.46803 19.99829 -2.7733 -1.1682 NaN NaN 1.6051 NaN ISPIN_1/bands
2 C2 105.49325 2.46803 2.46803 19.99829 4.5161 4.5591 -9.8405 -12.2355 0.0430 2.3950 ISPIN_2/bands
{% endraw %} {% raw %}
print(df.data.to_latex())
\begin{tabular}{llrrrrrrrrrrl}
\toprule
{} & sys &          V &        a &        b &         c &     VBM &     CBM &  so\_max &   so\_min &   E\_gap &    Δ\_SO &           rel\_path \\
\midrule
0 &  C2 &  105.49325 &  2.46803 &  2.46803 &  19.99829 & -3.9468 & -0.8125 & -3.9491 &   4.7810 &  3.1343 & -8.7301 &  ISPIN\_1/bands/DOS \\
1 &  C2 &  105.49325 &  2.46803 &  2.46803 &  19.99829 & -2.7733 & -1.1682 &     NaN &      NaN &  1.6051 &     NaN &      ISPIN\_1/bands \\
2 &  C2 &  105.49325 &  2.46803 &  2.46803 &  19.99829 &  4.5161 &  4.5591 & -9.8405 & -12.2355 &  0.0430 &  2.3950 &      ISPIN\_2/bands \\
\bottomrule
\end{tabular}

{% endraw %} {% raw %}
ax = pp.init_figure()
df = df.data # Access to Data if there is caption in Dataframe
df.sort_values('VBM').plot(ax=ax,x = 'VBM',y=['CBM','E_gap'])
<matplotlib.axes._subplots.AxesSubplot at 0x283f03cbc88>
{% endraw %} {% raw %}
import pivotpy as pp
print(', '.join(pp.__all__))
Dict2Data, Dic2Dot, read_asxml, exclude_kpts, get_ispin, get_summary, get_kpts, get_tdos, get_evals, get_bands_pro_set, get_dos_pro_set, get_structure, export_vasprun, load_export, dump_dict, get_file_size, interpolate_data, ps_to_py, ps_to_std, select_dirs, select_files, get_child_items, invert_color, printr, printg, printb, printy, printm, printc, EncodeFromNumpy, DecodeToNumpy, link_to_class, plot_bands, modify_axes, quick_bplot, add_text, add_legend, add_colorbar, create_rgb_lines, quick_rgb_lines, quick_color_lines, init_figure, select_pdos, collect_dos, quick_dos_lines, plt_to_html, get_rgb_data, flip_even_patches, rgb_to_plotly, plotly_to_html, plotly_rgb_lines, plotly_dos_lines, iplotfromtxt, save_mp_API, load_mp_data, get_crystal, get_poscar, get_kpath, get_kmesh, intersect_3p_p_3v, centroid, order, in_vol_sector, out_bz_plane, to_xy, rad_angle, arctan_full, get_bz, plot_bz, light_style, dark_style, get_files_gui, get_input_gui, read_data, click_data, tabulate_data, save_data, color_toggle, clrear_cache, matplotlib_code, generate_summary, show_app, show, savefig
{% endraw %} {% raw %}
import os 
os.chdir('E:/Research/graphene_example/ISPIN_1/bands')
xml_data=pp.read_asxml()
vr=pp.export_vasprun(elim=[-5,5])
vr
Data(
    sys_info = Data(
        SYSTEM = C2
        NION = 2
        TypeION = 1
        ElemName = ['C']
        ElemIndex = [0, 2]
        E_Fermi = -3.35005822
        ISPIN = 1
        fields = ['s', 'py', 'pz', 'px', 'dxy', 'dyz', 'dz2', 'dxz', 'x2-y2']
        incar = Data(
            SYSTEM = C2
            PREC = high
            ALGO = N
            LSORBIT = T
            NELMIN = 7
            ISMEAR = 0
            SIGMA = 0.10000000
            LORBIT = 11
            KPOINT_BSE = -1     0     0     0
            GGA = PS
        )
    )
    dim_info = Data(
        ⇅ = Each of SpinUp/SpinDown Arrays
        kpoints = (NKPTS,3)
        kpath = (NKPTS,1)
        bands = ⇅(NKPTS,NBANDS)
        dos = ⇅(grid_size,3)
        pro_dos = ⇅(NION,grid_size,en+pro_fields)
        pro_bands = ⇅(NION,NKPTS,NBANDS,pro_fields)
    )
    kpoints = <ndarray:shape=(90, 3)>
    kpath = <list:len=90>
    bands = Data(
        E_Fermi = -3.35005822
        ISPIN = 1
        NBANDS = 10
        bands_range = range(4, 14)
        evals = <ndarray:shape=(90, 10)>
    )
    tdos = Data(
        E_Fermi = -3.35005822
        ISPIN = 1
        grid_range = range(124, 203)
        tdos = <ndarray:shape=(79, 3)>
    )
    pro_bands = Data(
        labels = ['s', 'py', 'pz', 'px', 'dxy', 'dyz', 'dz2', 'dxz', 'x2-y2']
        pros = <ndarray:shape=(2, 90, 10, 9)>
    )
    pro_dos = Data(
        labels = ['energy', 's', 'py', 'pz', 'px', 'dxy', 'dyz', 'dz2', 'dxz', 'x2-y2']
        pros = <ndarray:shape=(2, 79, 10)>
    )
    poscar = Data(
        volume = 105.49324928
        basis = <ndarray:shape=(3, 3)>
        rec_basis = <ndarray:shape=(3, 3)>
        positions = <ndarray:shape=(2, 3)>
    )
)
{% endraw %}

Matplotlib's static plots

{% raw %}
import pivotpy as pp 
import matplotlib.pyplot as plt 
vr1=pp.export_vasprun('E:/Research/graphene_example/ISPIN_2/bands/vasprun.xml')
vr2=pp.export_vasprun('E:/Research/graphene_example/ISPIN_2/dos/vasprun.xml')
axs=pp.init_figure(ncols=3,widths=[1,1,1],sharey=True,wspace=0.05,figsize=(10,2.6))
elements=[0,0,[0,1]]
orbs=[[0],[1],[2,3]]
orblabels=['s','p_z','(p_x+p_y)']
ti_cks=dict(xt_indices=[0,30,60,-1],xt_labels=['Γ','M','K','Γ'])
args_dict=dict(elements=elements,orbs=orbs,labels=orblabels,elim=[-20,15])
pp.quick_bplot(path_evr=vr1,ax=axs[0],**ti_cks,elim=[-20,15])
pp.quick_rgb_lines(path_evr=vr1,ax=axs[1],**args_dict,**ti_cks,colorbar=False)
lg_k={'ncol': 3}
pp.quick_dos_lines(path_evr=vr2,ax=axs[2],vertical=True,include_dos='pdos',**args_dict,legend_kwargs=lg_k)
pp.add_colorbar(ax=plt.gcf().add_axes([0.399,1.02,0.23,0.05]),ticklabels=[r'$s^{⇅}$',r'$p_z^{⇅}$',r'$(p_x+p_y)^{⇅}$'])
pp.show() 
{% endraw %}

Interactive plots using plotly

{% raw %}
from IPython.display import HTML
fig = pp.plotly_rgb_lines(vr1,**args_dict)
#HTML(fig.to_html())
{% endraw %}

Brillouin Zone (BZ) Processing

  • Look in pivotpy.sio module for details on generating mesh and path of KPOINTS as well as using Materials Projects' API to get POSCAR right in the working folder with command get_poscar. Below is a screenshot of interactive BZ plot. You can double click on blue points and hit Ctrl + C to copy the high symmetry points relative to reciprocal lattice basis vectors. (You will be able to draw kpath in Pivotpy-Dash application and generate KPOINTS automatically from a web interface later on!).
  • Same color points lie on a sphere, with radius decreasing as red to blue and gamma point in gold color. These color help distinguishing points but the points not always be equivalent, for example in FCC, there are two points on mid of edges connecting square-hexagon and hexagon-hexagon at equal distance from center but not the same points.
  • Any colored point's hover text is in gold background.
{% raw %}
import pivotpy as pp 
from IPython.display import HTML,display
fig = pp.plot_bz([[1,0,0],[0,1,0],[0,0,1]])
HTML(fig.to_html())
{% endraw %}

Plotting Two Calculations Side by Side

  • Here we will use shift_kpath to demonstrate plot of two calculations on same axes side by side
{% raw %}
import matplotlib.pyplot as plt
import pivotpy as pp 
plt.style.use('bmh')
vr1=pp.export_vasprun('E:/Research/graphene_example/ISPIN_1/bands/vasprun.xml')
shift_kpath=vr1.kpath[-1] # Add last point from first export in second one.
vr2=pp.export_vasprun('E:/Research/graphene_example/ISPIN_2/bands/vasprun.xml',shift_kpath=shift_kpath)
last_k=vr2.kpath[-1]
axs=pp.init_figure(figsize=(5,2.6))
K_all=[*vr1.kpath,*vr2.kpath] # Merge kpath for ticks
kticks=[K_all[i] for i in [0,30,60,90,120,150,-1]]
ti_cks=dict(xticks=kticks,xt_labels=['Γ','M','K','Γ','M','K','Γ'])
pp.quick_bplot(path_evr=vr1,ax=axs)
pp.quick_bplot(path_evr=vr2,ax=axs,txt='Graphene(Left: ISPIN=1, Right: ISPIN=2)',ctxt='m')
pp.modify_axes(ax=axs,xlim=[0,last_k],ylim=[-10,10],**ti_cks)
{% endraw %}

Interpolation

{% raw %}
import pivotpy as pp
plt.style.use('ggplot')
k=vr1.kpath
ef=vr1.bands.E_Fermi
evals=vr1.bands.evals-ef
#Let's interpolate our graph to see effect. It is useful for colored graphs.
knew,enew=pp.interpolate_data(x=k,y=evals,n=10,k=3)
plot=plt.plot(k,evals,'m',lw=5,label='real data')
plot=plt.plot(k,evals,'w',lw=1,label='interpolated',ls='dashed')
pp.add_text(ax=plt.gca(),txts='Graphene')
{% endraw %}

Running powershell commands from python.

Some tasks are very tideious in python while just a click way in powershell. See below, and try to list processes in python yourself to see the difference!

{% raw %}
pp.ps_to_std(ps_command='(Get-Process)[0..4]')
NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
------    -----      -----     ------      --  -- -----------
53    39.77      18.03     901.41   13988   1 AltC
38    40.05      33.10      45.00     792   1 ApplicationFrameHost
8     1.64       4.39       0.00    7532   0 AppVShNotify
8     1.88       4.60       0.09   18180   1 AppVShNotify
19     4.77       4.37       0.00    4992   0 armsvc
{% endraw %}